<?php

class Admin_LoginController extends Zend_Controller_Action {
	public function indexAction() {
		$form = new My_Forms_Admin_Login ( );
		if ($this->_request->isPost ()) {
			$formData = $this->_request->getPost ();
			if ($form->isValid ( $formData )) {
				
				Zend_Debug::dump ( $formData, "login_message", false );
				//				die ();
				$db = Zend_Registry::get ( 'db' );
				$authAdapter = new Zend_Auth_Adapter_DbTable ( $db );
				$authAdapter->setTableName ( 'bc_users' );
				$authAdapter->setIdentityColumn ( 'username' );
				$authAdapter->setCredentialColumn ( 'password' );
				$authAdapter->setCredentialTreatment ( 'MD5(?)' );
				
				// Set the input credential values to authenticate against
				$authAdapter->setIdentity ( $formData ["userName"] );
				$authAdapter->setCredential ( $formData ["passWord"] );
				
				// do the authentication
				$auth = Zend_Auth::getInstance ();
				$result = $auth->authenticate ( $authAdapter );
				Zend_Debug::dump ( $result, "authAdapter", false );
				//				die ();
				if ($result->isValid ()) {
					// success : store database row to auth's storage system
					// (not the password though!)
					$data = $authAdapter->getResultRowObject ( null, 'password' );
					$auth->getStorage ()->write ( $data );
					//					echo $data;
					//					var_dump($auth);
					echo $result->getIdentity () . "\n\n";
					$identity = $result->getIdentity ();
					Zend_Debug::dump ( $data->id, "id", false );
					
					$this->_redirect ( '/admin/index' );
				} else {
					$this->view->message = '您的用户名或密码不对.';
				}
			} else {
				$form->populate ( $formData );
			}
		}
		$this->view->form = $form;
		Zend_Debug::dump ( $form->__toString (), "form", false );
	}
	function logoutAction() {
		Zend_Auth::getInstance ()->clearIdentity ();
		$this->_redirect ( '/admin' );
	}
}
